let db = require('../db/index')

exports.getList = (req, res) => {
  const info = req.query
  let start = Number(info.limit) * (Number(info.page) - 1)
  let length = Number(info.limit)
  let count = 0

  let sql = `select * from article`
  let limit = ` order by pubTime desc limit ${start},${length}`

  let sqlCount = `select count(*) from article`

  info.cate && (sql += ` where category = '${info.cate}'`,sqlCount  += ` where category = '${info.cate}'`)
  info.words && (sql += ` where title like '%${info.words}%'` , sqlCount += ` where title like '%${info.words}%'`)

  db.query(sqlCount, (err, data) => {
    count = data[0]['count(*)']
    db.query(sql + limit, (err, data) => {
      if (err) {
        return res.send('获取失败' + err.message)
      } else {
        res.send({
          code: 20000,
          data: { data, count }
        })
      }
    })
  })
}

exports.addInfo = (req, res) => {
  const info = req.body
  const sql = 'insert into article (id,banner,pubTime,title,render,content,summary,category,publisher,publisherId) value (?,?,?,?,?,?,?,?,?,?)'

  db.query(sql, [info.id, info.banner, info.pubTime, info.title, info.render, info.content, info.summary, info.category, info.publisher, info.publisherId], (err, data) => {
    if (err) {
      return res.send('添加失败' + err.message)
    }
    res.send({
      code: 20000,
      data: '添加成功'
    })
  })
}

exports.getInfo = (req, res) => {
  const id = req.params.id
  const sql = 'select * from article where id = ?'
  const sql2 = 'update article set viewsCount = viewsCount + 1 where id = ?'
  const sql3 = 'select * from article where id = ? and viewsCount >= 100'
  const sql4 = 'update article set isHot = 1 where id = ?'

  db.query(sql, id, (err, data) => {
    if (err) {
      return res.send('获取失败' + err.message)
    }

    db.query(sql2, id, (err, result) => {
      if (err) {
        return res.send('获取失败' + err.message)
      }

      db.query(sql3, id, (err, result) => {
        if (err) {
          return res.send({
            code: 20000,
            data: data[0]
          })
        }
        if (result.length === 0) {
          res.send({
            code: 20000,
            data: data[0]
          })
        }
        if (result.length > 0) {
          db.query(sql4, id, (err, result) => {
            if (err) {
              return err
            }
            return res.send({
              code: 20000,
              data: data[0]
            })
          })
        }

      })
    })
  })
}

exports.updateInfo = (req, res) => {
  const info = req.body
  const sql = 'update article set banner = ?,pubTime = ?,title = ?,render = ?,content = ?,summary = ?,category = ? where id = ?'
  db.query(sql, [info.banner, info.pubTime, info.title, info.render, info.content, info.summary, info.category, info.id], (err, data) => {
    if (err) {
      return res.send('修改失败' + err.message)
    }
    res.send({
      code: 20000,
      data: '修改成功'
    })
  })
}

exports.deleteInfo = (req, res) => {
  const id = req.params.id
  const sql = 'delete from article where id = ?'
  db.query(sql, id, (err, data) => {
    if (err) {
      return res.send('删除失败' + err.message)
    }
    res.send({
      code: 20000,
      data: '删除成功'
    })
  })
}

exports.getUserList = (req, res) => {
  const id = req.params.id

  let sql = `select * from article where publisherId = ?  order by id desc`

  db.query(sql,id, (err, data) => {
    if (err) {
      return res.send('获取失败' + err.message)
    } else {
      res.send({
        code: 20000,
        data: data
      })
    }
  })
}